package cn.t12.qg.orderconsumer.service;

import cn.t12.qg.orderconsumer.vo.OrderVo;
import cn.t12.qg.service.user.GoodsService;
import cn.t12.qg.service.user.OrderService;
import com.alibaba.fastjson.JSONObject;
import com.qg.common.Constants;
import com.qg.pojo.QgGoods;
import com.qg.pojo.QgOrder;
import com.qg.pojo.QgUser;
import com.qg.utils.RedisUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class LocalOrderServiceImpl implements LocalOrderService {

    @DubboReference(version = "1.0.0")
    private OrderService orderService;

    @DubboReference(version = "1.0.0")
    private GoodsService goodsService;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public List queryOrderList(String token) {
        List list = new ArrayList();
        String jsonUserInfo = redisUtil.getStr(Constants.tokenPrefix + token);
        QgUser qgUser = JSONObject.parseObject(jsonUserInfo,QgUser.class);
        List<QgOrder> qgOrderList =  orderService.listOrderByUserId(qgUser.getId());
        if(qgOrderList != null){
            OrderVo orderVo;
            for (QgOrder qgOrder : qgOrderList) {
                orderVo = new OrderVo();
                BeanUtils.copyProperties(qgOrder,orderVo);
                QgGoods qgGoods = goodsService.getById(qgOrder.getGoodsId());
                orderVo.setGoodsImg(qgGoods.getGoodsImg());
                list.add(orderVo);
            }
        }
        return list;
    }
}
